package org.clockworkmages.games.anno1186;

import java.util.List;

import org.clockworkmages.games.anno1186.model.situation.ExplorableGameEvent;
import org.clockworkmages.games.anno1186.model.situation.Situation;
import org.clockworkmages.games.anno1186.model.situation.SituationPoolForwarder;

public class SituationPoolService {

	@Injected
	private GameDataService gameDataService;

	public Situation getRandomSituationFromPool(String situationPoolId) {

		List<ExplorableGameEvent> situationPool = gameDataService.getGameData()
				.getEventPools().get(situationPoolId);

		// TODO: must be enhanced using probability and conditions:
		int selectedIndex = (int) Math.floor(Math.random()
				* situationPool.size());

		ExplorableGameEvent explorableEvent = situationPool.get(selectedIndex);

		if (explorableEvent instanceof Situation) {
			return (Situation) explorableEvent;
		} else if (explorableEvent instanceof SituationPoolForwarder) {
			return getRandomSituationFromPool(explorableEvent
					.getAreaType());
		} else {
			// TODO exception, not supported
			return null;
		}

	}

}
